﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIZMA._controls;
using BIZModel;
using BL.BIZControls;

namespace BIZMA._controls.DemoProject
{
    public partial class Sel_Customer : System.Web.UI.UserControl
    {
        #region Defaultddl
        public ListItem SelectedItem { get { return ddl.SelectedItem; } }
        public Guid SelectedId { get { return Guid.Parse(string.IsNullOrEmpty(ddl.SelectedValue) ? Guid.Empty.ToString() : ddl.SelectedValue); } set { ddl.SelectedValue = value.ToString();} }
        public string SelectedValue { get { return ddl.SelectedValue; } set { ddl.SelectedValue = value; } }
        public string Description { get { return lbl.Text; } set { lbl.Text = value; } }
        public string ToolTip { get { return lbl.ToolTip; } set { lbl.ToolTip = value; ddl.ToolTip = value; } }

        public bool Enabled { get { return ddl.Enabled; } set { ddl.Enabled = value; } }
        public bool AutoPostBack { get { return ddl.AutoPostBack; } set { ddl.AutoPostBack = value; } }
        public DropDownList DropDown { get { return ddl; } }

        public delegate void SelectedIndexChanged(DropDownList sender, EventArgs e, Guid selectedId);
        public event SelectedIndexChanged SelectedChanged;

        private Guid CurrentId { get { return ViewState["CurrentId"] != null ? (Guid)ViewState["CurrentId"] : Guid.Empty; } set { ViewState["CurrentId"] = value; } }

        public bool InitLoaded { get { return ViewState["InitLoaded"] != null ? (bool)ViewState["InitLoaded"] : false; } set { ViewState["InitLoaded"] = value; } }
        public bool EditFunction { get { return btnOpen.Visible; } set { btnOpen.Visible = value; } }

        public string ReqValidationGroup { get { return reqVal.ValidationGroup; } set { reqVal.ValidationGroup = value; } }
        public bool ReqValEnabled { get { return reqVal.Enabled; } set { reqVal.Enabled = value; } }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack || !InitLoaded)
                LoadList();

        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (ToolTip == "")
                ToolTip = Description;

            if (reqVal.IsValid)
            {
                lbl.CssClass = lbl.CssClass.Replace(" error", "");
                ddl.CssClass = ddl.CssClass.Replace(" error", "");
            }
            else
            {
                lbl.CssClass += " error";
                ddl.CssClass += " error";
            }

            lbl.ID = ddl.ID + "_label";

            base.Render(writer);
        }

        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (SelectedChanged != null)
            {
                SelectedId = Guid.Parse(((DropDownList)sender).SelectedValue);
                SelectedChanged((DropDownList)sender, e, SelectedId);
            }
        }
        #endregion

        #region Modal
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            btnOpen.Visible = EditFunction && mContainer.CanAddPopupControl(BIZControl.DEMOCustomer);
            mContainer.ButtonsClick += new ButtonsHandler(mContainer_ButtonsClick);
            mContainer.ModalClosing += new ModalControlClosing(mContainer_ModalClosing);
        }

        void mContainer_ModalClosing(object sender, EventArgs e)
        {
            LoadList();
        }

        void mContainer_ButtonsClick(object sender, ActionType actionType)
        {

        }

        protected void UC_ModalContainer1_UserControlClosing(object sender, EventArgs e)
        {

        }

        protected void Open_Click(object sender, EventArgs e)
        {
            
            if (ddl.SelectedValue == "0")
            {
                mContainer.AddDefaultEditNew(BIZControl.DEMOCustomer);
            }
            else
            {
                SelectedId = Guid.Parse(ddl.SelectedValue);
                mContainer.AddDefaultEditLoad(BIZControl.DEMOCustomer, SelectedId);
            }

            //mContainer.AddControl(BIZControl.DemoBusinessType);

            //if (mContainer.ContainedControlExists)
            //{

            //    mContainer.ModalOpen();

            //    if (ddl.SelectedValue == "0")
            //    {
            //        ((Edit_BusinessType)mContainer.ContainedControl).NewBusinessType();

            //    }
            //    else
            //    {
            //        SelectedId = Guid.Parse(ddl.SelectedValue);
            //        CurrentId = Guid.Parse(ddl.SelectedValue);
            //        ((Edit_BusinessType)mContainer.ContainedControl).LoadBusinessType(Guid.Parse(ddl.SelectedValue));
            //    }
            //}

        }
        #endregion

        public void ReloadBusinessTypes()
        {
            LoadList();
        }

        private void LoadList()
        {
            InitLoaded = true;
            BIZMAEntities ent = BaseBL.GetEnt();

            reqVal.InitialValue = Guid.Empty.ToString();

            ddl.Items.Clear();
            ddl.AppendDataBoundItems = true;

            ddl.Items.Add(new ListItem("--- Välj (ny)---", Guid.Empty.ToString()));
            ddl.AppendDataBoundItems = true;
            ddl.DataTextField = "Name";
            ddl.DataValueField = "CustomerId";

            var data = from BIZModel.Customer q in ent.Customers.OrderBy(q => q.Name)
                       select new { q.Name, q.CustomerId };
            
            ddl.DataSource = data;
            ddl.DataBind();

            if (CurrentId != Guid.Empty)
                ddl.SelectedValue = CurrentId.ToString();
        }





    }
}